#import "Core.h"

@interface PrayViewController(private)

- (void)initSubView;
- (void)initContentView;
- (void)initTabBarView;

- (void)nineDayPrayTouched:(id)sender;
- (void)rosarioPrayTouched:(id)sender;
//- (void)prayTextTouched:(id)sender;
- (void)prayAlarmTouched:(id)sender;
- (void)praySettingTouched:(id)sender;

@end

@implementation PrayViewController(private)

- (void)initSubView
{
	_contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 380)];
	_tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 430, 320, 50)];
	
	[self initContentView];
	[self initTabBarView];
	
	_contentView.backgroundColor = [UIColor blackColor];
	_tabBarView.backgroundColor = [UIColor blackColor];
	
	[self.view addSubview:_contentView];
	[self.view addSubview:_tabBarView];
	
	[CaulyViewController initCauly:self setLogLevel:CL_NOLOG];
	if( [CaulyViewController requestBannerADWithViewController:self xPos:0 yPos:382 adType:BT_IPHONE] == FALSE ) {
		NSLog(@"requestBannerAD failed");
	}
}

- (void)initContentView
{
	_nineDayNavigationController = [[NineDayNavigationController alloc] initWithRootViewController:self];//initWithPurpose:@"밥주세요"];
	_rosarioNavigationController = [[RosarioNavigationController alloc] initWithRootViewController:self];
	_prayAlarmNavigationController = [[PrayAlarmNavigationController alloc] initWithRootViewController:self];
	_praySettingNavigationController = [[PraySettingNavigationController alloc] initWithRootViewController:self];
	
	[_contentView addSubview:_rosarioNavigationController.view];
}

- (void)initTabBarView
{
	_rosarioPray	= [[ImageButton alloc] initWithFrame:CGRectMake(PRAYVIEW_ROSARIOPRAY_LEFT, 
																	PRAYVIEW_ROSARIOPRAY_TOP, 
																	PRAYVIEW_ROSARIOPRAY_WIDTH, 
																	PRAYVIEW_ROSARIOPRAY_HEIGHT) 
					   andShowThisImageForNormalState:[UIImage imageNamed:PRAYVIEW_IMG_ROSARIOPRAY_ON]
						  thenShowThisWhenHighlighted:[UIImage imageNamed:PRAYVIEW_IMG_ROSARIOPRAY_ON]
												  let:self 
										handleEventAt:@selector(rosarioPrayTouched:)];
	_nineDayPray	= [[ImageButton alloc] initWithFrame:CGRectMake(PRAYVIEW_NINEDAYPRAY_LEFT, 
																	PRAYVIEW_NINEDAYPRAY_TOP, 
																	PRAYVIEW_NINEDAYPRAY_WIDTH, 
																	PRAYVIEW_NINEDAYPRAY_HEIGHT) 
					   andShowThisImageForNormalState:[UIImage imageNamed:PRAYVIEW_IMG_NINEDAYPRAY_OFF] 
						  thenShowThisWhenHighlighted:[UIImage imageNamed:PRAYVIEW_IMG_NINEDAYPRAY_ON] 
												  let:self 
										handleEventAt:@selector(nineDayPrayTouched:)];
	_prayAlarm		= [[ImageButton alloc] initWithFrame:CGRectMake(PRAYVIEW_PRAYALARM_LEFT, 
																	PRAYVIEW_PRAYALARM_TOP, 
																	PRAYVIEW_PRAYALARM_WIDTH, 
																	PRAYVIEW_PRAYALARM_HEIGHT) 
					  andShowThisImageForNormalState:[UIImage imageNamed:PRAYVIEW_IMG_PRAYALARM_OFF]
						 thenShowThisWhenHighlighted:[UIImage imageNamed:PRAYVIEW_IMG_PRAYALARM_ON]
												 let:self 
									   handleEventAt:@selector(prayAlarmTouched:)];
	
	_praySetting	= [[ImageButton alloc] initWithFrame:CGRectMake(PRAYVIEW_PRAYSETTING_LEFT, 
																	PRAYVIEW_PRAYSETTING_TOP, 
																	PRAYVIEW_PRAYSETTING_WIDTH, 
																	PRAYVIEW_PRAYSETTING_HEIGHT) 
					   andShowThisImageForNormalState:[UIImage imageNamed:PRAYVIEW_IMG_PRAYSETTING_OFF]
						  thenShowThisWhenHighlighted:[UIImage imageNamed:PRAYVIEW_IMG_PRAYSETTING_ON]
												  let:self 
										handleEventAt:@selector(praySettingTouched:)];
	
	[_tabBarView addSubview:_nineDayPray];
	[_tabBarView addSubview:_rosarioPray];
	[_tabBarView addSubview:_prayAlarm];
	[_tabBarView addSubview:_praySetting];
}

- (void)nineDayPrayTouched:(id)sender
{
	[_nineDayPray	setNormalImage:[UIImage imageNamed:PRAYVIEW_IMG_NINEDAYPRAY_ON]];
	[_nineDayPray	setHighlightedImage:[UIImage imageNamed:PRAYVIEW_IMG_NINEDAYPRAY_ON]];
	[_rosarioPray	setNormalImage:[UIImage imageNamed:PRAYVIEW_IMG_ROSARIOPRAY_OFF]];
	[_rosarioPray	setHighlightedImage:[UIImage imageNamed:PRAYVIEW_IMG_ROSARIOPRAY_ON]];
	[_prayAlarm		setNormalImage:[UIImage imageNamed:PRAYVIEW_IMG_PRAYALARM_OFF]];
	[_prayAlarm		setHighlightedImage:[UIImage imageNamed:PRAYVIEW_IMG_PRAYALARM_ON]];
	[_praySetting	setNormalImage:[UIImage imageNamed:PRAYVIEW_IMG_PRAYSETTING_OFF]];
	[_praySetting	setHighlightedImage:[UIImage imageNamed:PRAYVIEW_IMG_PRAYSETTING_ON]];
	
	[_contentView addSubview:_nineDayNavigationController.view];
	
	if( [_rosarioNavigationController.view superview] != nil )
		[_rosarioNavigationController.view removeFromSuperview];
	if( [_prayAlarmNavigationController.view superview] != nil )
		[_prayAlarmNavigationController.view removeFromSuperview];
	if( [_praySettingNavigationController.view superview] != nil )
		[_praySettingNavigationController.view removeFromSuperview];
	
	[CaulyViewController hideBannerAD:NO];
}

- (void)rosarioPrayTouched:(id)sender
{
	[_nineDayPray	setNormalImage:[UIImage imageNamed:PRAYVIEW_IMG_NINEDAYPRAY_OFF]];
	[_nineDayPray	setHighlightedImage:[UIImage imageNamed:PRAYVIEW_IMG_NINEDAYPRAY_ON]];
	[_rosarioPray	setNormalImage:[UIImage imageNamed:PRAYVIEW_IMG_ROSARIOPRAY_ON]];
	[_rosarioPray	setHighlightedImage:[UIImage imageNamed:PRAYVIEW_IMG_ROSARIOPRAY_ON]];
	[_prayAlarm		setNormalImage:[UIImage imageNamed:PRAYVIEW_IMG_PRAYALARM_OFF]];
	[_prayAlarm		setHighlightedImage:[UIImage imageNamed:PRAYVIEW_IMG_PRAYALARM_ON]];
	[_praySetting	setNormalImage:[UIImage imageNamed:PRAYVIEW_IMG_PRAYSETTING_OFF]];
	[_praySetting	setHighlightedImage:[UIImage imageNamed:PRAYVIEW_IMG_PRAYSETTING_ON]];	
	
	[_contentView addSubview:_rosarioNavigationController.view];
	
	if( [_nineDayNavigationController.view superview] != nil )
		[_nineDayNavigationController.view removeFromSuperview];
	if( [_prayAlarmNavigationController.view superview] != nil )
		[_prayAlarmNavigationController.view removeFromSuperview];
	if( [_praySettingNavigationController.view superview] != nil )
		[_praySettingNavigationController.view removeFromSuperview];
	
	[CaulyViewController hideBannerAD:NO];
}

- (void)prayAlarmTouched:(id)sender
{
	[_nineDayPray	setNormalImage:[UIImage imageNamed:PRAYVIEW_IMG_NINEDAYPRAY_OFF]];
	[_nineDayPray	setHighlightedImage:[UIImage imageNamed:PRAYVIEW_IMG_NINEDAYPRAY_ON]];
	[_rosarioPray	setNormalImage:[UIImage imageNamed:PRAYVIEW_IMG_ROSARIOPRAY_OFF]];
	[_rosarioPray	setHighlightedImage:[UIImage imageNamed:PRAYVIEW_IMG_ROSARIOPRAY_ON]];
	[_prayAlarm		setNormalImage:[UIImage imageNamed:PRAYVIEW_IMG_PRAYALARM_ON]];
	[_prayAlarm		setHighlightedImage:[UIImage imageNamed:PRAYVIEW_IMG_PRAYALARM_ON]];
	[_praySetting	setNormalImage:[UIImage imageNamed:PRAYVIEW_IMG_PRAYSETTING_OFF]];
	[_praySetting	setHighlightedImage:[UIImage imageNamed:PRAYVIEW_IMG_PRAYSETTING_ON]];	
	
	[_contentView addSubview:_prayAlarmNavigationController.view];

	if( [_nineDayNavigationController.view superview] != nil )
		[_nineDayNavigationController.view removeFromSuperview];
	if( [_rosarioNavigationController.view superview] != nil )
		[_rosarioNavigationController.view removeFromSuperview];
	if( [_praySettingNavigationController.view superview] != nil )
		[_praySettingNavigationController.view removeFromSuperview];
	
	[CaulyViewController hideBannerAD:YES];
}

- (void)praySettingTouched:(id)sender
{
	[_nineDayPray	setNormalImage:[UIImage imageNamed:PRAYVIEW_IMG_NINEDAYPRAY_OFF]];
	[_nineDayPray	setHighlightedImage:[UIImage imageNamed:PRAYVIEW_IMG_NINEDAYPRAY_ON]];
	[_rosarioPray	setNormalImage:[UIImage imageNamed:PRAYVIEW_IMG_ROSARIOPRAY_OFF]];
	[_rosarioPray	setHighlightedImage:[UIImage imageNamed:PRAYVIEW_IMG_ROSARIOPRAY_ON]];
	[_prayAlarm		setNormalImage:[UIImage imageNamed:PRAYVIEW_IMG_PRAYALARM_OFF]];
	[_prayAlarm		setHighlightedImage:[UIImage imageNamed:PRAYVIEW_IMG_PRAYALARM_ON]];
	[_praySetting	setNormalImage:[UIImage imageNamed:PRAYVIEW_IMG_PRAYSETTING_ON]];
	[_praySetting	setHighlightedImage:[UIImage imageNamed:PRAYVIEW_IMG_PRAYSETTING_ON]];	
	
	[_contentView addSubview:_praySettingNavigationController.view];

	if( [_nineDayNavigationController.view superview] != nil )
		[_nineDayNavigationController.view removeFromSuperview];
	if( [_rosarioNavigationController.view superview] != nil )
		[_rosarioNavigationController.view removeFromSuperview];
	if( [_prayAlarmNavigationController.view superview] != nil )
		[_prayAlarmNavigationController.view removeFromSuperview];
	
	[CaulyViewController hideBannerAD:NO];
}

@end


@implementation PrayViewController

- (void)viewDidLoad {
	PraySettingData*	settingData = [PraySettingData getSettingDataFromDefaultPath];

	if( settingData.language == nil )
	{
		NSString* language = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] objectAtIndex:0];
		
		if( [language isEqualToString:@"es"] || [language isEqualToString:@"kr"] )
			settingData.language = language;
		else
			settingData.language = @"en";
	}
	
	_languageBundle = [[NSBundle alloc] initWithPath:[[NSBundle mainBundle] pathForResource:@"language" ofType:@"bundle"]];
		
	[PraySettingData saveSettingDataToDefaultPath:settingData];
	[self initSubView];
}


/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
	
	[_contentView release];
	[_tabBarView release];
	[_nineDayPray release];
	[_rosarioPray release];
//	[_prayText release];
	[_prayAlarm release];
	[_praySetting release];
}

- (NSString*)getLocalizedStringForKey:(NSString *)key
{
	NSString* langCode = [PraySettingData getSettingDataFromDefaultPath].language;

	if ( ![langCode isEqualToString:@"ko"] )
	{
		NSString* tableName = [NSString stringWithFormat:@"texts-%@", langCode];
		NSString* retStr = [_languageBundle localizedStringForKey:key value:@"STRING NOT FOUND" table:tableName];
		
		if ( nil != retStr && ![retStr isEqualToString:@"STRING NOT FOUND"] )
			return retStr;
	}
	
	return key;
}

#pragma mark PraySettingViewControllerDelegate methods

- (void)onSettingChanged:(PraySettingData *)setting
{
	[_nineDayNavigationController onSettingChanged:setting];
	[_rosarioNavigationController onSettingChanged:setting];
}

#pragma mark CaulyProtocol method

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// 주! 아래 카울리 프로토콜 구현 메소드의 리턴값에 특수문자나 공백이 들어가 있으면 광고정보를 정상적으로 못 얻어 옵니다.    //
////////////////////////////////////////////////////////////////////////////////////////////////////////

- (NSString *) devKey 
{
	return @"9일기도"; //CAULY는 테스트용 개발코드입니다. 제품 배포시에는 발급받은 어플리케이션 고유개발코드로 변환해 주세요.
}

// 성별 값을 리턴합니다. "male", "female", "all"
- (NSString *) gender
{
	return @"all";	// 공백이나 특수문자는 사용할 수 없습니다.
}

// 나이 값을 리턴합니다. "all", "10", "20", "30", "40", "50"
- (NSString *) age
{
	return @"all";	// 공백이나 특수문자는 사용할 수 없습니다.
}

// 카울리 시스템이 위치 정보를 수집하도록 설정합니다.
// 카울리 시스템이 위치 정보를 수집할 경우 사용자에 맞는 광고를 내 보내주게 되고 이를 통해 광고 수입이 늘어 날 수 있게 됩니다.
- (BOOL) getGPSInfo
{
	//	return TRUE; // 리턴값을 TRUE로 하게되면 사용자들로 하여금 위치 정보 승인을 물어보게 됩니다.
	return FALSE;
}

// 광고 교체 주기값을 설정합니다.
- (REFRESH_PERIOD) rollingPeriod {
	return SEC_60;
}

// 광고가 바뀔 때의 효과를 설정합니다.
- (ANIMATION_TYPE) animationType {
	return FADEOUT;
}

// 광고 데이터를 받고 나면 호출됩니다.
- (void)AdReceiveCompleted {
	//NSLog(@"AdReceiveCompleted..");
}

// 광고 데이터를 받는데 실패하면 호출됩니다.
- (void)AdReceiveFailed {
	//NSLog(@"AdReceiveFailed..");
}


// 전면 광고를 종료시키면 호출됩니다.
// 만약 전면 광고를 요청하지 않을 경우 이 메소드는 호출되지 않습니다.
// 전면 광고와 배너 광고를 동시에 노출 시킬 수 없으므로 전면 광고가 종료된 뒤 배너 광고를 노출시키기 바랍니다.
- (void)closeCPMAd {
	/*
	NSLog(@"배너광고 노출");
	
	// 이 예제의 경우 requestBannerADWithViewController 메소드를 이용하였습니다.
	// 만약 viewController를 사용하지 않는 앱의 경우 아래와 같이 requestBannerADWithView메소드를 이용하시기 바랍니다.
	// 예) [CaulyViewController requestBannerADWithView:parentView xPos:0 yPos:0]
	
	//	if( [CaulyViewController requestBannerADWithView:viewController.view xPos:0 yPos:0 adType:BT_IPHONE] == FALSE ) {
	if( [CaulyViewController requestBannerADWithViewController:viewController xPos:0 yPos:0 adType:BT_IPHONE] == FALSE ) {
		NSLog(@"requestBannerAD failed");
	}
	
	// 배너 광고 회전 예제
	//	if( [CaulyViewController rotateCaulyBannerView:UIInterfaceOrientationLandscapeRight] == FALSE ) {
	//		NSLog(@"caulyView rotation failed");
	//	}
	*/
}

@end
